#include <iostream>
#include <deque>
#include <cstring>
using namespace std;

const int N = 510;

char p[N][N];
int f[N][N];
int n,m;

int x1,y1,x2,y2;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
void bfs()
{
	memset(f,-1,sizeof f);
	deque<pair<int,int>> q;
	f[x1][y1] = 0;
	q.push_front({x1,y1});
	while(q.size())
	{
		pair<int,int> r = q.front();
		q.pop_front();
		int x = r.first;
		int y = r.second;
		if(x == x2 && y == y2)
		{
			return;
		}
		for(int i = 0; i < 4; i++)
		{
			int a = x + dx[i];
			int b = y + dy[i];
			if(a < 0 || b < 0 || a >= n || b >= m) continue;
			int s = 0;
			if(p[a][b] != p[x][y]) s = 1;
			if(f[a][b] == -1) 
			{
				f[a][b] = f[x][y] + s;
				if(s == 0)
					q.push_front({a,b});
				else
					q.push_back({a,b});
			}
			else if(f[a][b] > f[x][y] + s)
			{
				f[a][b] = f[x][y] + s;
//				if(s == 0)
//					q.push_front({a,b});
//				else
//					q.push_back({a,b});
			}
		}
	}
	
}

int main()
{
	while(cin >> n >> m, n != 0 || m != 0)
	{
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < m; j++)
			{
				cin >> p[i][j];	
			}
		}
		
		cin >> x1 >> y1 >> x2 >> y2;
		bfs();
		printf("%d\n",f[x2][y2]);
	}
	return 0;
}
